package com.bjesc_b2c.app.appraiset.utils;

import android.text.TextUtils;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import master_bean.CarForBean;

/**
 * Created by yidong on 2017/7/26.
 */

public class LocalGroupSearch {

    public static ArrayList<CarForBean> searchGroup(CharSequence str,
                                               ArrayList<CarForBean> allContacts) {
        ArrayList<CarForBean> groupList = new ArrayList<CarForBean>();

        CharacterParser finder = CharacterParser.getInstance();

        String result = "";
        for (CarForBean group : allContacts) {
            // 先将输入的字符串转换为拼音
            finder.setResource(str.toString());
            result = finder.getSpelling();
//            if (contains(group, result)) {
//                groupList.add(group);
//            } else
                if ((group.getMarket_name() + "").contains(str)) {
                groupList.add(group);
            } else if ((group.getName() + "").contains(str)) {
                groupList.add(group);
            }
        }
        return groupList;
    }
    public static ArrayList<CarForBean> searchDealerBytype(CharSequence str,
                                                    ArrayList<CarForBean> allContacts,int type) {
        ArrayList<CarForBean> groupList = new ArrayList<CarForBean>();

        CharacterParser finder = CharacterParser.getInstance();

        String result = "";
        for (CarForBean group : allContacts) {
            // 先将输入的字符串转换为拼音
            finder.setResource(str.toString());
            result = finder.getSpelling();
//            if (contains(group, result)) {
//                groupList.add(group);
//            } else
            if (type ==1) {
                if ((group.getName() + "").contains(str)) {
                    groupList.add(group);
                }
            }else if ((group.getBusiness_agent() + "").contains(str)) {
                groupList.add(group);
            }
        }
        return groupList;
    }
    /**
     * 根据拼音搜索
     *
     *            搜索条件是否大于6个字符
     * @return
     */
    private static boolean contains(CarForBean group, String search) {
        if (TextUtils.isEmpty(group.getName())) {
            return false;
        }

        boolean flag = false;

        // 简拼匹配,如果输入在字符串长度大于6就不按首字母匹配了
        if (search.length() < 6) {
            String firstLetters = group.getFirstLetter();
            // 不区分大小写
            Pattern firstLetterMatcher = Pattern.compile(search,
                    Pattern.CASE_INSENSITIVE);
            flag = firstLetterMatcher.matcher(firstLetters).find();
        }

        if (!flag) { // 如果简拼已经找到了，就不使用全拼了
            // 全拼匹配
            CharacterParser finder = CharacterParser.getInstance();
            finder.setResource(getGroupName(group));
            // 不区分大小写
            Pattern pattern2 = Pattern
                    .compile(search, Pattern.CASE_INSENSITIVE);
            Matcher matcher2 = pattern2.matcher(finder.getSpelling());
            flag = matcher2.find();
        }

        return flag;
    }

    private static String getGroupName(CarForBean group) {
        String strName = null;
        if (!TextUtils.isEmpty(group.getName())) {
            strName = group.getName();
        } else if (!TextUtils.isEmpty(group.getMarket_name())) {
            strName = group.getMarket_name();
        } else {
            strName = "";
        }
        return strName;
    }
}
